﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
namespace TestDxfDocument
{
    class center_pin
    {
        public static void _center_pin(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-----------------------------------------------------------------------------------------------------
            double scale = 1; //放大系数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            var type = /*data["type"].ToString()*/"E型";

            //定义参数
            /*double a = 5.5 * scale;
            double b = 18 * scale;
            double c = 3.64 * scale;
            double d = 5 * scale;
            double e = 3.3 * scale;
            double f = 7.7 * scale;*/

            double a = Convert.ToDouble(data["a"]) * scale;
            double b = Convert.ToDouble(data["b"]) * scale;
            double c = Convert.ToDouble(data["c"]) * scale;
            double d = Convert.ToDouble(data["d"]) * scale;
            double e = Convert.ToDouble(data["e"]) * scale;
            double f = Convert.ToDouble(data["f"]) * scale;

            /*if (type.Equals("P型"))
            {
                a = 5.3 * scale;
                b = 5.16 * scale;
                c = 7.4 * scale;
                d = 3.0 * scale;
                e = 3.3 * scale;
            }*/

            if (type.Equals("E型"))
            {
                //定义点-----------------------------------------------------------------------------------------------------------------------------------
                Vector2 v1 = new Vector2(0, 0);
                Vector2 v2 = new Vector2(v1.X + b * 1.2, v1.Y);
                Vector2 v3 = new Vector2(v1.X + (fun.CalDistance(v1, v2) / 2 - b / 2), v1.Y + d / 2 * 0.8);
                Vector2 v4 = new Vector2(v3.X + f * 0.4, v3.Y);
                Vector2 v5 = new Vector2(v4.X, v1.Y + d / 2);
                Vector2 v6 = new Vector2(v5.X + f * 0.9, v5.Y);
                Vector2 v7 = new Vector2(v6.X, v4.Y);
                Vector2 v8 = new Vector2(v5.X + f, v7.Y);
                Vector2 v9 = new Vector2(v8.X, v1.Y + a / 2);
                Vector2 v10 = new Vector2(v9.X + (b - e - f - fun.CalDistance(v3, v4)), v9.Y);
                Vector2 v11 = fun.mirrorpoint(v3, v1, v2);
                Vector2 v12 = fun.mirrorpoint(v4, v1, v2);
                Vector2 v13 = fun.mirrorpoint(v5, v1, v2);
                Vector2 v14 = fun.mirrorpoint(v6, v1, v2);
                Vector2 v15 = fun.mirrorpoint(v7, v1, v2);
                Vector2 v16 = fun.mirrorpoint(v8, v1, v2);
                Vector2 v17 = fun.mirrorpoint(v9, v1, v2);
                Vector2 v18 = fun.mirrorpoint(v10, v1, v2);
                Vector2 v19 = new Vector2(v10.X, v10.Y - (a - c) + c * 0.1);
                Vector2 v20 = new Vector2(v19.X + e, v19.Y);
                Vector2 v21 = new Vector2(v20.X, v20.Y - c);
                Vector2 v22 = new Vector2(v19.X, v21.Y);

                //作图部分-------------------------------------------------------------------------------------------------
                //中心线
                Line centre_line1 = new Line(v1, v2);
                centre_line1.Layer = centre;
                centre_line1.Linetype = Linetype.Center;
                centre_line1.LinetypeScale = 1;
                dxf.AddEntity(centre_line1);

                //轮廓线
                LwPolyline polyline1 = new LwPolyline();
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                polyline1.Vertexes.Add(new LwPolylineVertex(v4));
                polyline1.Vertexes.Add(new LwPolylineVertex(v5));
                polyline1.Vertexes.Add(new LwPolylineVertex(v6));
                polyline1.Vertexes.Add(new LwPolylineVertex(v7));
                polyline1.Vertexes.Add(new LwPolylineVertex(v8));
                polyline1.Vertexes.Add(new LwPolylineVertex(v9));
                polyline1.Vertexes.Add(new LwPolylineVertex(v10));
                polyline1.Vertexes.Add(new LwPolylineVertex(v18));
                polyline1.Vertexes.Add(new LwPolylineVertex(v17));
                polyline1.Vertexes.Add(new LwPolylineVertex(v16));
                polyline1.Vertexes.Add(new LwPolylineVertex(v15));
                polyline1.Vertexes.Add(new LwPolylineVertex(v14));
                polyline1.Vertexes.Add(new LwPolylineVertex(v13));
                polyline1.Vertexes.Add(new LwPolylineVertex(v12));
                polyline1.Vertexes.Add(new LwPolylineVertex(v11));
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                fun.PolyFill(dxf, polyline1, 0.3, contour);

                LwPolyline polyline2 = new LwPolyline();
                polyline2.Vertexes.Add(new LwPolylineVertex(v4));
                polyline2.Vertexes.Add(new LwPolylineVertex(v12));
                fun.PolyFill(dxf, polyline2, 0.3, contour);

                LwPolyline polyline3 = new LwPolyline();
                polyline3.Vertexes.Add(new LwPolylineVertex(v7));
                polyline3.Vertexes.Add(new LwPolylineVertex(v15));
                fun.PolyFill(dxf, polyline3, 0.3, contour);

                LwPolyline polyline4 = new LwPolyline();
                polyline4.Vertexes.Add(new LwPolylineVertex(v8));
                polyline4.Vertexes.Add(new LwPolylineVertex(v16));
                fun.PolyFill(dxf, polyline4, 0.3, contour);

                LwPolyline polyline5 = new LwPolyline();
                polyline5.Vertexes.Add(new LwPolylineVertex(v19));
                polyline5.Vertexes.Add(new LwPolylineVertex(v20));
                polyline5.Vertexes.Add(new LwPolylineVertex(v21));
                polyline5.Vertexes.Add(new LwPolylineVertex(v22));
                fun.PolyFill(dxf, polyline5, 0.3, contour);

                //标注------------------------------------------------------------------------------------------
                //定义
                double height = 1;
                double arrowsize = 1;
                double exline = 1;

                //第一部分---------------------------------------------------
                //直线标注
                //上侧
                fun.dimrot1(dxf, v12, v10, 0, a, 1, height, arrowsize, exline, "f=" + Math.Round(f, 1), "", "");
                //左侧
                fun.dimrot1(dxf, v6, v15, 90, f / 3, 1, height, arrowsize, exline, "d=" + Math.Round(d, 1), "", "");
                //右侧
                fun.dimrot1(dxf, v10, v18, -90, f * 1.5, 1, height, arrowsize, exline, "a=" + Math.Round(a, 1), "", "");
                fun.dimrot1(dxf, v20, v21, -90, f * 0.5, 1, height, arrowsize, exline, "c=" + Math.Round(c, 1), "", "");
                //下侧
                fun.dimrot1(dxf, v21, v18, 180, c, 1, height, arrowsize, exline, "e=" + Math.Round(e, 1), "", "");
                fun.dimrot1(dxf, v11, v21, 180, a * 1.5, 1, height, arrowsize, exline, "b=" + Math.Round(b, 1), "", "");
            }

            if (type.Equals("P型"))
            {
                //定义点-----------------------------------------------------------------------------------------------------------------------------------
                Vector2 v1 = new Vector2(0, 0);
                Vector2 v2 = new Vector2(v1.X + c * 1.2, v1.Y);
                Vector2 v3 = new Vector2(v1.X + (fun.CalDistance(v1, v2) - c) / 2, v1.Y + b / 2);
                Vector2 v4 = new Vector2(v3.X + (c - d) * 0.9 / 2.0 - 0.5, v3.Y);
                Vector2 v5 = fun.Polar(v4, -45 * pi / 180, 0.5 / Math.Cos(45 * pi / 180));
                Vector2 v6 = fun.Polar(v5, 45 * pi / 180, 0.5 / Math.Cos(45 * pi / 180));
                Vector2 v7 = new Vector2(v3.X + (c - d) * 0.9, v3.Y);
                Vector2 v9 = new Vector2(v7.X, v1.Y + a / 2);
                Vector2 v10 = new Vector2(v3.X + (c - d), v9.Y);
                Vector2 v11 = new Vector2(v10.X, v10.Y - (a - e) * 2);
                Vector2 v12 = new Vector2(v11.X + fun.CalDistance(v9, v10), v11.Y);
                Vector2 v13 = new Vector2(v12.X, v1.Y + e / 2);
                Vector2 v14 = new Vector2(v10.X + d, v13.Y);
                Vector2 v15 = fun.mirrorpoint(v14, v1, v2);
                Vector2 v16 = fun.mirrorpoint(v13, v1, v2);
                Vector2 v17 = fun.mirrorpoint(v12, v1, v2);
                Vector2 v18 = fun.mirrorpoint(v11, v1, v2);
                Vector2 v19 = fun.mirrorpoint(v10, v1, v2);
                Vector2 v20 = fun.mirrorpoint(v9, v1, v2);
                Vector2 v21 = fun.mirrorpoint(v7, v1, v2);
                Vector2 v22 = fun.mirrorpoint(v3, v1, v2);

                //作图部分-------------------------------------------------------------------------------------------------
                //中心线
                Line centre_line1 = new Line(v1, v2);
                centre_line1.Layer = centre;
                centre_line1.Linetype = Linetype.Center;
                centre_line1.LinetypeScale = 1;
                dxf.AddEntity(centre_line1);

                //轮廓线
                LwPolyline polyline1 = new LwPolyline();
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                polyline1.Vertexes.Add(new LwPolylineVertex(v4));
                polyline1.Vertexes.Add(new LwPolylineVertex(v5));
                polyline1.Vertexes.Add(new LwPolylineVertex(v6));
                polyline1.Vertexes.Add(new LwPolylineVertex(v7));
                polyline1.Vertexes.Add(new LwPolylineVertex(v9));
                polyline1.Vertexes.Add(new LwPolylineVertex(v10));
                polyline1.Vertexes.Add(new LwPolylineVertex(v11));
                polyline1.Vertexes.Add(new LwPolylineVertex(v12));
                polyline1.Vertexes.Add(new LwPolylineVertex(v13));
                polyline1.Vertexes.Add(new LwPolylineVertex(v14));
                polyline1.Vertexes.Add(new LwPolylineVertex(v15));
                polyline1.Vertexes.Add(new LwPolylineVertex(v16));
                polyline1.Vertexes.Add(new LwPolylineVertex(v17));
                polyline1.Vertexes.Add(new LwPolylineVertex(v18));
                polyline1.Vertexes.Add(new LwPolylineVertex(v19));
                polyline1.Vertexes.Add(new LwPolylineVertex(v20));
                polyline1.Vertexes.Add(new LwPolylineVertex(v21));
                polyline1.Vertexes.Add(new LwPolylineVertex(v22));
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                fun.PolyFill(dxf, polyline1, 0.1, contour);

                LwPolyline polyline2 = new LwPolyline();
                polyline2.Vertexes.Add(new LwPolylineVertex(v7));
                polyline2.Vertexes.Add(new LwPolylineVertex(v21));
                fun.PolyFill(dxf, polyline2, 0.1, contour);

                //标注------------------------------------------------------------------------------------------
                //定义
                double height = 1;
                double arrowsize = 1;
                double exline = 1;

                //第一部分---------------------------------------------------
                //直线标注
                //左侧
                fun.dimrot1(dxf, v3, v22, 90, d, 1, height, arrowsize, exline, "b=" + Math.Round(b, 1), "", "");
                //右侧
                fun.dimrot1(dxf, v14, v15, -90, d * 2, 1, height, arrowsize, exline, "e=" + Math.Round(e, 1), "", "");
                fun.dimrot1(dxf, v10, v19, -90, d * 5, 1, height, arrowsize, exline, "a=" + Math.Round(a, 1), "", "");
                //下侧
                fun.dimrot1(dxf, v19, v15, 180, e, 1, height, arrowsize, exline, "d=" + Math.Round(d, 1), "", "");
                fun.dimrot1(dxf, v22, v15, 180, a * 1.5, 1, height, arrowsize, exline, "c=" + Math.Round(c, 1), "", "");
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
